<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动标题" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>



    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['points:product:add']"
        >添加砍价商品</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['points:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="砍价图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="砍价名称" align="center" prop="name" />
      <el-table-column label="砍价价格" align="center" prop="startPrice" />
      <el-table-column label="最低价" align="center" prop="minPrice" />
      <el-table-column label="参与人数" align="center" prop="needAllFrequency" />
      <el-table-column label="帮忙砍价人数" align="center" prop="helpBargain" />
      <el-table-column label="限量总数" align="center" prop="sillSum" />
      <el-table-column label="限量剩余" align="center" prop="sillNum" />
      <el-table-column label="活动状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status===1?"开启":"关闭" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.id)"
            v-hasPermi="['points:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="copyHandle(scope.row.id,'copy')"
            v-hasPermi="['points:product:edit']"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="statisticsHandle(scope.row.id)"
            v-hasPermi="['points:product:edit']"
          >统计</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['points:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/points/pointsProduct";
import {delBargainProduct, listBargainProduct} from "@/api/points/bargainProduct";

export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 砍价兑换商品功能表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        img: null,
        name: null,
        canGetNum: null,
        weight: null,
        needPoints: null,
        sillSum: null,
        sillNum: null,
        isHot: null,
        status: null,
        proId: null,
        specificationId: null,
        htmlId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    statisticsHandle(id){
      this.$router.push({
        path: '/bargain/statistics',
        query: {
          id: id
        }
      })
    },
    copyHandle(id,copy){
      this.reset();
      this.$router.push({
        path: '/bargain/addBargainProduct',
        query: {
          id:id,
          copy:copy
        }
      });
    },
    /** 查询砍价兑换商品功能列表 */
    getList() {
      this.loading = true;
      listBargainProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        img: null,
        name: null,
        canGetNum: null,
        weight: null,
        needPoints: null,
        sillSum: null,
        sillNum: null,
        createTime: null,
        isHot: null,
        status: null,
        proId: null,
        specificationId: null,
        htmlId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.$route.path
      this.$router.push({
        path: '/bargain/addBargainProduct',
        // 添加不传参数
        // query: {
        //   id: id,
        // }
      });
      this.title = "添加砍价兑换商品功能";
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      this.$router.push({
        path: '/bargain/addBargainProduct',
        query: {
          id:id,
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除砍价兑换商品功能编号为"' + ids + '"的数据项？').then(function() {
        return delBargainProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('points/bargainProduct/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
